package Selenium.XMLReaders;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

//This class is also included in the Global variable class so that
//it can have a global presence
//This class will be initialized only once and will be changed after each test

public class XMLResultWriter {
	
	public String XmlFilePath;
	private DocumentBuilderFactory XMLDocumentFactory;
	private DocumentBuilder XMLDocumentBuilder;
	private File XMLFile;
	private Document Doc;
	
	
	public XMLResultWriter(String XMLFilepath)
	{
		this.XmlFilePath = XMLFilepath;
		//XMLFile = new File(XmlFilePath);
		XMLDocumentFactory = DocumentBuilderFactory.newInstance();
		//create XML document builder
		try 
		{
			XMLDocumentBuilder = XMLDocumentFactory.newDocumentBuilder();
			
		} catch (ParserConfigurationException e) {
			System.out.println("Error was raised while building XML document");
		}		
		Doc = XMLDocumentBuilder.newDocument();
	}
	
	
	//Use this to write the head of the XML File
	public boolean WriteXMLHead(String BuildId, String DateOfRun)
	{
		Element TestResultNode;
		Attr Attribute;
		
		try
		{
				///Setting the Build Id attribute
				TestResultNode = Doc.createElement("TestResult");
				Attribute = Doc.createAttribute("BuildId");
				Attribute.setValue(BuildId);
				TestResultNode.setAttributeNode(Attribute);
				
				//Setting the Date of run attribute
				Attribute = null;
				Attribute = Doc.createAttribute("DateOfRun");
				Attribute.setValue(DateOfRun);
				TestResultNode.setAttributeNode(Attribute);
		return true;
		}
				catch(Exception e)
		{
			return false;
		}
		
		
			
	}
	
	
}
